package org.linlinjava.litemall.wx.service;

import static org.linlinjava.litemall.wx.util.WxResponseCode.COMMON_DETAIL_IS_NULL;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.linlinjava.litemall.core.util.ConvertUtils;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.constant.DBConmonConstant;
import org.linlinjava.litemall.db.service.SupplyService;
import org.linlinjava.litemall.wx.vo.QualificationsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.hutool.core.map.MapUtil;

@Service
public class WxSupplyService {
	@Autowired
	private SupplyService supplyService;
	@Autowired
	private WxQualificationsService wxQualificationsService;

	public Object list(Integer userId, Byte type, Integer page, Integer limit, String sort, String order,
			String searchValue) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		QualificationsVo qualificationsVo = wxQualificationsService.getQualificationsVo(userId);
		if (qualificationsVo == null) {
			return ResponseUtil.fail(COMMON_DETAIL_IS_NULL, "未获取到资质信息，请刷新重试");
		}

		List<String> querys = Lists.newArrayList();
		if (!StringUtils.isEmpty(searchValue)) {
			querys.add(
					" CONCAT(IFNULL(fs.herb_name,''),IFNULL(fs.herb_describe,''),IFNULL(fs.herb_origin,''),IFNULL(fs.contact_name,''),IFNULL(fs.contact_tel,'')) LIKE '%"
							+ searchValue + "%' ");
		}
		querys.add(" fs.status = 1 ");
		//querys.add(" fs.due_time > now() ");//排除过期的供应数据
		querys.add(" fs.deleted = 0 ");
		//TODO
		/*switch(DBConmonConstant.UserType.getEnumByValue(qualificationsVo.getUserType())) {
		case Personal:
			//个人登录情况下，默认只能看到自己的供应信息
			querys.add(" u.id = " + userId + " ");
			break;
		case Manufactor:
			//厂家登录情况下，默认只能看到个人和自己的供应信息
			querys.add(" (fs.user_type = 0 or u.id = " + userId + ") ");
			break;
		case Distributor:
			//供应商登录情况下，默认只能看到厂家的供应信息
			querys.add(" fs.user_type = 1 ");
			break;
		default:
			break;
		}*/
		
		String query = StringUtils.collectionToDelimitedString(querys, "and");
		String orderByClause = null;
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			orderByClause = " fs.id desc ";
		}

		PageHelper.startPage(page, limit);
		List<Map> mapList = supplyService.getSupplys(query, orderByClause);
		Page<Map> list = (Page) mapList;

		Map<String, Object> data = Maps.newHashMap();
		data.put("list", ConvertUtils.sourceToCamelCaseMap(list));
		data.put("total", list.getTotal());
		data.put("page", list.getPageNum());
		data.put("limit", list.getPageSize());
		data.put("pages", list.getPages());
		return ResponseUtil.ok(data);
	}

	public Object myList(Integer userId, Byte type, Integer page, Integer limit, String sort, String order,String searchValue) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		List<String> querys = Lists.newArrayList();
		if (!StringUtils.isEmpty(searchValue)) {
			querys.add(
					" CONCAT(IFNULL(fs.herb_name,''),IFNULL(fs.herb_describe,''),IFNULL(fs.herb_origin,''),IFNULL(fs.contact_name,''),IFNULL(fs.contact_tel,'')) LIKE '%"
							+ searchValue + "%' ");
		}
		querys.add(" u.id = " + userId + " ");
		querys.add(" fs.deleted = 0 ");
		// 状态切换 TODO
		switch (type) {
		case 1:// 待审核
			querys.add(" fs.status = 0 ");
			break;
		case 2:// 已审核
			querys.add(" fs.status = 1 ");
			break;
		case 3:// 被驳回
			querys.add(" fs.status = 2 ");
			break;
		case 4:// 已完结
			querys.add(" fs.status = 3 ");
			break;
		}
		String query = StringUtils.collectionToDelimitedString(querys, "and");
		String orderByClause = null;
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			orderByClause = " fs.id desc ";
		}

		PageHelper.startPage(page, limit);
		List<Map> mapList = supplyService.getSupplys(query, orderByClause);
		Page<Map> list = (Page) mapList;

		Map<String, Object> data = Maps.newHashMap();
		data.put("list", ConvertUtils.sourceToCamelCaseMap(list));
		data.put("total", list.getTotal());
		data.put("page", list.getPageNum());
		data.put("limit", list.getPageSize());
		data.put("pages", list.getPages());
		return ResponseUtil.ok(data);
	}

	public Object detail(Integer userId, Integer id) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		Map<String, Object> data = Maps.newHashMap();
		List<String> querys = Lists.newArrayList();
//		querys.add(" u.id = " + userId + " ");
		querys.add(" fs.id = " + id + " ");
		String query = StringUtils.collectionToDelimitedString(querys, "and");
		List<Map> mapList = supplyService.getSupplys(query, null);
		if (CollectionUtils.isNotEmpty(mapList)) {
			data.put("supply", MapUtil.toCamelCaseMap(mapList.get(0)));
		} else {
			return ResponseUtil.fail(COMMON_DETAIL_IS_NULL, "供应详情不存在或已被删除");
		}
		return ResponseUtil.ok(data);
	}
}